const app = getApp();
var openid = wx.getStorageSync('openid');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    hasMore: true,
    subtitle: '',
    contentList: [],
    page: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.initData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.initData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.loadMore();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /*初始化列表数据*/
  initData: function() {
    this.showLoading();
    var that = this;
    //重置页码为1
    that.setData({
      page: 1,

    });
    wx.request({
      url: app.config.serverCfg.domain.dynamicMain,
      method: 'post',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        openid: openid,
        page: that.data.page,
      },
      success: function(res) {
        that.hideLoading();
        that.setData({
          contentList: res.data.data
        })
      }
    });
  },
  /*加载更多*/
  loadMore() {
    var that = this;
    this.showLoading();
    var hasMore = true;
    wx.request({
      url: app.config.serverCfg.domain.dynamicMain,
      method: 'post',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        openid: openid,
        page: ++that.data.page,
      },
      success: function(res) {
        if (res.data.data.length == 0) {
          hasMore = false;
        }
        that.data.contentList = that.data.contentList.concat(res.data.data);
        that.hideLoading();
        that.setData({
          contentList: that.data.contentList,
          hasMore: hasMore,
        })
      }
    });
  },
  /**
   * 对内容进行点赞
   */
  doLike: function(e) {
    var openid = wx.getStorageSync('openid');
    var common = require('../../utils/common.js');
    if (!openid) {
      common.getToLogin();
      return false;
    }
    var that = this;
    var like = 1;
    if (e.currentTarget.dataset.like === true) {
      like = -1;
    }
    //先静态改变数据
    for (var i = 0; i < that.data.contentList.length; i++) {
      if (that.data.contentList[i]['id'] == e.currentTarget.dataset.id) {
        var indexLike = 'contentList[' + i + '].isLike';
        var indexCount = 'contentList[' + i + '].count_like';
        this.setData({
          [indexLike]: !e.currentTarget.dataset.like,
          [indexCount]: that.data.contentList[i]['count_like'] -1+1+like,//+是拼接，需要减一下
        });
      }
    }
    //修改数据库的数据
    wx.request({
      url: app.config.serverCfg.domain.likeAdd,
      method: 'post',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        openid: openid,
        like: like,
        type:1,//代表动态
        extendId: e.currentTarget.dataset.id
      },
      success: function(res) {}
    });
  },
  showLoading() {
    wx.showNavigationBarLoading();
    this.setData({
      subtitle: '加载中...',
      loading: true,
    });
  },
  //隐藏加载效果
  hideLoading: function() {
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
    this.setData({
      loading: false
    });
  },
  //分享效果
  onShareAppMessage: function(e) {
    wx.request({
      url: app.config.serverCfg.domain.shareAdd,
      method: 'post',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        type: 1,//代表动态
        extendId: e.target.dataset.id
      },
    });
    let that = this;
    return {
      title: e.target.dataset.title, // 转发后 所显示的title
      path: '/pages/dynamic/detail/detail', // 相对的路径
      success: (res) => {
        wx.showToast({
          title: '分享成功',
          icon: 'none',
          duration: 1000
        })
        //分享后进行计数
        wx.request({
          url: app.config.serverCfg.domain.shareAdd,
          method: 'post',
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          data: {
            type: 1,//代表动态
            extendId: e.target.dataset.id
          },
        });
        // 成功后要做的事情
        wx.getShareInfo({
          success: (res) => {},
          fail: function(res) {
          },
          complete: function(res) {
          }
        })
      },
      fail: function(res) {
      }
    }
  },
  //点击图片进行放大显示
  priviewImage: function(event) {
    var src = event.currentTarget.dataset.src; //获取data-src
    var index = event.currentTarget.dataset.images; 
    var imgList = this.data.contentList[index]['pics'] //获取data-list
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  }
})